import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from bs4 import BeautifulSoup

import requests
from .lent import MySql

class Day16(unittest.TestCase):
    def setUp(self):
        self.mydb = MySql('root','root','127.0.0.1','day03')
    def tearDown(self):
        self.mydb.close()
        print('结束')

    def test_db_01(self):

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0 Safari/537.36'
        }
        response = requests.get('https://movie.douban.com/top250', headers=headers)
        response.encoding = 'utf-8'
        soup = BeautifulSoup(response.text, 'html.parser')
        movie_list = soup.find_all('div', class_='item')[:10]  # 取前10个电影
        for movie in movie_list:
            name = movie.find('span', class_='title').get_text()

            self.mydb.insert(name)
            print(f"存入电影: {name} ")


if __name__ == '__main__':
    unittest.main()
